package com.gdk.filter;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.gdk.service.LoginService;



/**
 * 过滤除admin的所有人
 * */
public class LoginFilter implements Filter {
	
	//UserService userService = new UserServiceImpl();
/*	ApplicationContext context = new ClassPathXmlApplicationContext("classpath:Bean.xml");
	LoginService loginService = (LoginService) context.getBean("LoginService");
	*/
	/*
	 * 获取session
	 * 从session中获取admin
	 * 如果存在放行
	 * 不存在转发到login.jsp
	 * */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = req.getSession(); 
		//获取访问地址，如果是登录页面则放行
        String path = req.getRequestURI();
   //     System.out.println(path);
    //    System.out.println("session : " + session.getAttribute("root"));
   //     System.out.println("path.contains login.jsp : " +path.contains("/login.jsp"));
   //     System.out.println("path.contains loginVerify.do : " +path.contains("/loginVerify.do"));
   //     System.out.println("path.contains toIndex.do : " +path.contains("/toIndex.do"));
        
        if(path.contains("/login.jsp")||path.contains("/loginVerify.do")||path.contains("/toIndex.do") 
        		|| path.contains("/print_order_note.do")
        		){
    //        System.out.println("登录相关,放行");
        	
            chain.doFilter(req, res);
			
       //验证码图片和js放行
        }else if(session == null||session.getAttribute("root") == null){
	//		System.out.println("管理员未登录,过滤器成功拦截!");
		/*	if (path.endsWith(".css") || path.endsWith(".js")
					|| path.endsWith(".png") || path.endsWith(".jpg")
					|| path.endsWith(".gif") || path.endsWith(".html")
					|| path.endsWith(".ico")) {
				//System.out.println("通过");
				chain.doFilter(req, res);
				return;
			}*/
			if (path.contains(".css") || path.contains(".js")
					|| path.contains(".png") || path.contains(".jpg")
					|| path.contains(".gif") || path.contains(".html")
					|| path.contains(".ico")) {
		//		System.out.println("样式通过");
				chain.doFilter(req, res);
			     return;
			}
			//转发
			
				req.getRequestDispatcher("/toIndex.do").forward(req, res);
			//req.getRequestDispatcher("/jsp/login.jsp").forward(req, res);
			//重定向
			//res.sendRedirect(req.getContextPath()+"/jsp/login.jsp");
		     return;
        } else {
        	
       // 	System.out.println("管理员已登录,AdminFilter过滤器放行!");
        /*	Map<String,Object> user = (Map<String, Object>) session.getAttribute("user");
        	//查询出订货商申请通知
        	Map<String,Object> map = new HashMap<String,Object>();
			map.put("push_people",user.get("dd_user"));
			map.put("isread",0);
			map.put("push_type_id",1);
			map.put("ispc",1);
			*/
			//System.out.println("mapFilter : "+map);
			
        	chain.doFilter(req, res);
        }
	}
	public void init(FilterConfig fConfig) throws ServletException {
	}
	
	public void destroy() {

	}

}

